'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  
  async login() {
    let data = await this.ctx.service.home.login()
    this.ctx.body = data
  }

  async register() {
    let data = await this.ctx.service.home.register()
    this.ctx.body = data
  }
  async url() {
    // if (this.ctx.state.user.userid === 0) {
      this.ctx.body = {
        code: 0,
        data: [
          {
            path: '/homeIndex/table',
            text: '表格页面'
          },
          {
            path: '/homeIndex/form',
            text: '表单页面'
          },
          {
            path: '/homeIndex/list',
            text: '列表页面'
          },
          {
            path: '/homeIndex/echarts',
            text: '图表页面'
          }
        ]
      }
    // } else {
    //   this.ctx.body = {
    //     code: 0,
    //     data: [
    //       {
    //         path: '/homeIndex/table',
    //         text: '表格页面'
    //       },
    //       {
    //         path: '/homeIndex/echarts',
    //         text: '图表页面'
    //       }
    //     ]
    //   }
    // }
  }
  
  async search() {
     const { goodsname, startTime, endTime, state ='已' } = this.ctx.query;
     console.log(goodsname,startTime, endTime, state, '----params')
     const data = await this.app.mysql.query(`select * from goods where goodsname like '%${goodsname}%' and state like '%${state}%' and createtime between '${startTime}' and '${endTime}'`)
     this.ctx.body = data;
  }

  async findAll() {
    const data = await this.app.mysql.select('goods')
    this.ctx.body = data;
  }

  async update() {
    const id = this.ctx.params;
    const {  goodsname, state, count, price, remark } = this.ctx.request.body;
    const data = await this.app.mysql.update('goods', {
       id,
       goodsname,state, count,price, remark
    })
    this.ctx.body = data;
  }
  async add() {
    const {  goodsname, state, count, price, remark } = this.ctx.request.body;
    const data = await this.app.mysql.insert('goods', {
       goodsname,
       state, count,price, remark
    })
    this.ctx.body = data;
  }
  async delete() {
    const id = this.ctx.params;
    const data = await this.app.mysql.delete('goods', { id })
    this.ctx.body = data
  }

  async deleteAll() {
    const ids = this.ctx.params;
    const data = await this.app.mysql.query(`delete from goods where id in ?`,[ids])
    this.ctx.body = data
  }

  // select * from goods order by createtime desc;


  async page() {
    const { size, page } = this.ctx.query;
    const total = await this.app.mysql.select('goods');
    const data = await this.app.mysql.select('goods', { // 搜索 post 表=
      limit: size * 1, // 返回数据量
      offset: page * size * 1,   // 数据偏移量
      // order: [['createtime', 'desc']],
    });
    this.ctx.body = { data, total: total.length }
  }
}

module.exports = HomeController;
